package com.qinggeng.project.system.controller;

import com.qinggeng.common.utils.poi.ExcelUtil;
import com.qinggeng.framework.aspectj.lang.annotation.Log;
import com.qinggeng.framework.aspectj.lang.enums.BusinessType;
import com.qinggeng.framework.web.controller.BaseController;
import com.qinggeng.framework.web.domain.AjaxResult;
import com.qinggeng.framework.web.page.TableDataInfo;
import com.qinggeng.project.system.domain.OrdersGoods;
import com.qinggeng.project.system.service.IOrdersGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单商品Controller
 *
 * @author qinggeng
 * @date 2020-06-17
 */
@RestController
@RequestMapping("/system/orderGoods")
public class OrdersGoodsController extends BaseController {
    @Autowired
    private IOrdersGoodsService ordersGoodsService;

    /**
     * 查询订单商品列表
     */
    @PreAuthorize("@ss.hasPermi('system:goods:list')")
    @GetMapping("/list")
    public TableDataInfo list(OrdersGoods ordersGoods) {
        startPage();
        List<OrdersGoods> list = ordersGoodsService.selectOrdersGoodsList(ordersGoods);
        return getDataTable(list);
    }

    /**
     * 导出订单商品列表
     */
    @PreAuthorize("@ss.hasPermi('system:goods:export')")
    @Log(title = "订单商品", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(OrdersGoods ordersGoods) {
        List<OrdersGoods> list = ordersGoodsService.selectOrdersGoodsList(ordersGoods);
        ExcelUtil<OrdersGoods> util = new ExcelUtil<OrdersGoods>(OrdersGoods.class);
        return util.exportExcel(list, "goods");
    }

    /**
     * 获取订单商品详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:goods:query')")
    @GetMapping(value = "/{ordersGoodsId}")
    public AjaxResult getInfo(@PathVariable("ordersGoodsId") Long ordersGoodsId) {
        return AjaxResult.success(ordersGoodsService.selectOrdersGoodsById(ordersGoodsId));
    }

    /**
     * 新增订单商品
     */
    @PreAuthorize("@ss.hasPermi('system:goods:add')")
    @Log(title = "订单商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OrdersGoods ordersGoods) {
        return toAjax(ordersGoodsService.insertOrdersGoods(ordersGoods));
    }

    /**
     * 修改订单商品
     */
    @PreAuthorize("@ss.hasPermi('system:goods:edit')")
    @Log(title = "订单商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OrdersGoods ordersGoods) {
        return toAjax(ordersGoodsService.updateOrdersGoods(ordersGoods));
    }

    /**
     * 删除订单商品
     */
    @PreAuthorize("@ss.hasPermi('system:goods:remove')")
    @Log(title = "订单商品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ordersGoodsIds}")
    public AjaxResult remove(@PathVariable Long[] ordersGoodsIds) {
        return toAjax(ordersGoodsService.deleteOrdersGoodsByIds(ordersGoodsIds));
    }

    @GetMapping("/orderGoodsListByOrderId")
    @ResponseBody
    public AjaxResult getOrderGoodsList(Long orderId) {
        List<OrdersGoods> ordersGoodsList = ordersGoodsService.selectOrderGoodListByOrdersId(orderId);
        return AjaxResult.success(ordersGoodsList);
    }
}
